package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-11-24
 */
@RestController // RestControllerAdvice
@Api(tags = "讲师管理模块")
@CrossOrigin
@Slf4j
@RequestMapping("/admin/edu/teacher")  //管理员要考虑CRUD的权限管理、用户要考虑登录状态的验证+限流+黑名单白名单
public class AdminTeacherController {
    @Autowired
    TeacherService teacherService;
    //5、带条件的分页查询
    @ApiOperation(value = "分页查询讲师列表")
    @GetMapping("list/{pageNum}/{pageSize}")//必须通过 list/pageNum/pageSize 来访问 代表三级目录
    public R list(@ApiParam(value="页码" ,required = true ,defaultValue = "1") @PathVariable Integer pageNum ,
                  @ApiParam(value = "记录条数" , required = true ,defaultValue = "3" )@PathVariable Integer pageSize){
        IPage<Teacher> page = new Page<>(pageNum , pageSize);
        teacherService.page(page);
        return R.ok().data("page" , page);
    }
    //4、删除
    @DeleteMapping("deleteById/{id}")
    public R deleteById(@PathVariable String id){
        teacherService.removeById(id);
        return R.ok().message("删除成功");
    }
    //3、更新讲师
    @GetMapping("getById/{id}")
    public R getTeacher(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item",teacher);
    }
    @PutMapping("update")//get方式不能使用@RequestBody，没有请求体
    public R update(@RequestBody Teacher teacher){
        teacherService.updateById(teacher);
        return R.ok().message("更新成功");
    }


    //1、查询所有讲师
//    @ResponseBody
    @GetMapping("list")  //restController中的接口返回值会被转为json
    public R queryAll(){

       //{} 代表占位符
        log.debug("{}级别日志" , "debug");
        log.info("{}级别日志" , "info");
        log.warn("{}级别日志" , "warn");
        log.error("{}级别日志" , "error");


        // r = {code:xxx , message:xxx , success:true , data:  {items: [{},{}] } }
        //  r.data.items
        return R.ok().data("items",teacherService.list());
    }
    //2、新增讲师
    @PostMapping("save")
//    public String save(Teacher teacher){ //POJO入参  url?name=xx&age=xxx&xxx
    public R save(@RequestBody Teacher teacher){ //获取请求体参数： 以json的方式存到请求体中
        if(teacherService.save(teacher)){
            return R.ok();
        }
        return R.error();
    }
}

